package webgamemaker.Editor;

import webgamemaker.Common.Callback;
import webgamemaker.Common.GameMap;
import webgamemaker.Common.MapData;
import webgamemaker.Common.Tile;
import webgamemaker.Common.TileData;
import webgamemaker.Common.TileSet;
import webgamemaker.Editor.HistoryManager.HistoryAction;

import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.CloseClickHandler;
import com.smartgwt.client.widgets.tab.events.TabCloseClickEvent;

public class MainPanel extends TabSet 
{
	public MainPanel()
	{
		this.addCloseClickHandler(new CloseClickHandler()
			{
				public void onCloseClick(TabCloseClickEvent event) 
				{
					if(event.getTab() instanceof MapTab)
					{
						MapTab tab = (MapTab)event.getTab();
					}
				}
			});
	}
	
	public void setCursorSize(int width, int height)
	{
		for(Tab tab : getTabs())
			if(tab instanceof MapTab)
			{
				GameMap map = ((MapTab)tab).gameMap;
				map.cursorWidth = width;
				map.cursorHeight = height;
			}
	}
	
	public MapTab find(MapData mapData)
	{
		for(Tab tab : getTabs())
			if(tab instanceof MapTab && ((MapTab)tab).gameMap.mapData == mapData)
				return (MapTab)tab;
		return null;
	}
	
	public TileSetEditorTab find(TileSet tileset)
	{
		for(Tab tab : getTabs())
			if(tab instanceof TileSetEditorTab && ((TileSetEditorTab)tab).tilesetEditor.tileset == tileset)
				return (TileSetEditorTab)tab;
		return null;
	}
	
	public MapTab addMap(MapData mapData)
	{
		MapTab tab = new MapTab(mapData);
		addTab(tab);
		return tab;
	}
	
	public TileSetEditorTab addTileSetEditor(TileSet tileset)
	{
		TileSetEditorTab tab = new TileSetEditorTab(tileset);
		addTab(tab);
		return tab;
	}
	
	public class TileSetEditorTab extends Tab
	{
		public TileSetEditor tilesetEditor;
		
		public TileSetEditorTab(TileSet tileset)
		{
			setTitle("tileset "+tileset.name);
			tilesetEditor = new TileSetEditor(tileset);
			setCanClose(true);
			setPane(tilesetEditor);
		}
	}
	
	public class MapTab extends Tab
	{
		public GameMap gameMap;
		
		public MapTab(final MapData mapData)
		{
			setTitle(mapData.name);
			setCanClose(true);
			Canvas mapCanvas = new Canvas();
			gameMap = new GameMap(mapData);
			gameMap.redraw();
			mapCanvas.addChild(gameMap);
			setPane(mapCanvas);
			gameMap.addSelectCallback(new Callback()
			{
				public void onCall(Object src) 
				{
					TileSetPicker tilesetPicker = ProjectManager.getInstance().tilesetPanel.selectedTileSetPicker;
					final int id[] = new int[gameMap.cursorWidth * gameMap.cursorHeight];
					final TileData oldTile[] = new TileData[gameMap.cursorWidth * gameMap.cursorHeight];
					final TileData newTile[] = new TileData[gameMap.cursorWidth * gameMap.cursorHeight];
					final int layer = ProjectManager.getInstance().currentLayer;
					System.out.println("layer = "+layer);
					for(int i = 0; i < gameMap.cursorWidth; i++)
						for(int j = 0; j < gameMap.cursorHeight; j++)
						{
							id[i+j*gameMap.cursorWidth] = gameMap.cursorX+i+(gameMap.cursorY+j)*mapData.width;
							newTile[i+j*gameMap.cursorWidth] = tilesetPicker.tileset.tiles.get(tilesetPicker.selectX1+i+(tilesetPicker.selectY1+j)*tilesetPicker.nbTilePerLine);
							if(layer == -1)
								oldTile[i+j*gameMap.cursorWidth] = mapData.tiles[id[i+j*gameMap.cursorWidth]][newTile[i+j*gameMap.cursorWidth].default_layer];
							else oldTile[i+j*gameMap.cursorWidth] = mapData.tiles[id[i+j*gameMap.cursorWidth]][layer];
						}
					ProjectManager.getInstance().data.historyManager.makeAction(new HistoryAction()
						{
							public void redo() 
							{
								for(int i = 0; i < id.length; i++)
								{
									if(layer == -1)
										mapData.tiles[id[i]][newTile[i].default_layer] = newTile[i];
									else mapData.tiles[id[i]][layer] = newTile[i];
								}
								gameMap.redraw();
							}
							public void undo() 
							{
								for(int i = 0; i < id.length; i++)
								{
									if(layer == -1)
										mapData.tiles[id[i]][newTile[i].default_layer] = oldTile[i];
									else mapData.tiles[id[i]][layer] = oldTile[i];
								}
								gameMap.redraw();
							}
						});
				}
			});
		}
	}
}
